namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class bulletins
    {
        // Methods
        public static void Add(bulletin model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into bulletins(");
            builder.Append("[username],[category],[title],[body],[issuetime],[expiretime],[visible]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.category + "',");
            builder.Append("'" + model.title + "',");
            builder.Append("'" + model.body + "',");
            builder.Append("'" + model.issuetime + "',");
            builder.Append("'" + model.expiretime + "',");
            builder.Append("" + (model.visible ? 1 : 0) + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete bulletins ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from bulletins where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from bulletins ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static int GetMax(string field, string table, string where)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select max([" + field + "]) from [" + table + "] where " + where);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 1;
            }
            return int.Parse(scalar.ToString());
        }

        public static bulletin GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from bulletins ");
            builder.Append(" where id=" + id);
            bulletin bulletin = new bulletin();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            bulletin.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            bulletin.username = dataTable.Rows[0]["username"].ToString();
            bulletin.category = dataTable.Rows[0]["category"].ToString();
            bulletin.title = dataTable.Rows[0]["title"].ToString();
            bulletin.body = dataTable.Rows[0]["body"].ToString();
            if (dataTable.Rows[0]["issuetime"].ToString() != "")
            {
                bulletin.issuetime = DateTime.Parse(dataTable.Rows[0]["issuetime"].ToString());
            }
            if (dataTable.Rows[0]["expiretime"].ToString() != "")
            {
                bulletin.expiretime = DateTime.Parse(dataTable.Rows[0]["expiretime"].ToString());
            }
            if (dataTable.Rows[0]["visible"].ToString() != "")
            {
                if ((dataTable.Rows[0]["visible"].ToString() == "1") || (dataTable.Rows[0]["visible"].ToString().ToLower() == "true"))
                {
                    bulletin.visible = true;
                    return bulletin;
                }
                bulletin.visible = false;
            }
            return bulletin;
        }

        public static void Update(bulletin model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update bulletins set ");
            builder.Append("[username]='" + model.username + "',");
            builder.Append("[category]='" + model.category + "',");
            builder.Append("[title]='" + model.title + "',");
            builder.Append("[body]='" + model.body + "',");
            builder.Append("[issuetime]='" + model.issuetime + "',");
            builder.Append("[expiretime]='" + model.expiretime + "',");
            builder.Append("[visible]=" + (model.visible ? 1 : 0) + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }


}

